Springboot adjusts the interface response return time (solve the response timeout problem)
Springboot adjusts the interface/session response return time (solve the response timeout problem)
Configure HTTP session timeout
HTTP session timeout can be configured for Spring Boot applications in two ways .
Configure session timeout in application.properties
The easiest way is to add the parameter server.servlet.session.timeout to your application.properties . For example
server.servlet.session.timeout=60s
Also note that Tomcat does not allow you to set the timeout to less than 60 seconds.
Programmatically configure session timeout
Let's say we want our HttpSession to last only two minutes. To achieve this, we can add an EmbeddedServletContainerCustomizer Bean to our WebConfiguration class with the following content.
@Configuration
public class WebConfiguration {
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setSessionTimeout(2, TimeUnit.MINUTES);
}
};
}
}
Here is another shortcut using Java 8 and lambda expressions.
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return (ConfigurableEmbeddedServletContainer container) -> {
container.setSessionTimeout(2, TimeUnit.MINUTES);
};
}
During application startup, Spring Boot auto-configuration detects the EmbeddedServletContainerCustomizer and calls the customize(…) method, passing a reference to the Servlet container.
Configure interface access timeout
There are two ways to set interface access timeout in SpringBoot
1. Configuration file method
application.properties
Added in the configuration file spring.mvc.async.request-timeout=120000
, which means setting the timeout to 120000ms or 120s
# [设置接口的超时时间]
spring.mvc.async.request-timeout=120000
2. Configure the Config configuration class
Another way is to add in the config configuration class:
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(20000);
configurer.registerCallableInterceptors(timeoutInterceptor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
}
After adopting one of the two configurations above, re-run the service, and the maximum response time for calling the interface is the 120s set above.
Pitfalls to avoid
If a timeout still occurs after following the above configuration, it may be due to the following technical problems, which need to be set accordingly.
tomcat settings
The above is the springboot development environment, using the built-in tomcat. In actual production environments, external tomcat is generally used for deployment (to facilitate subsequent release and updates), and the timeout needs to be set in the tomcat configuration file server.xml (the default setting is 120 seconds or less for 20 seconds).
<Connector port="8811" protocol="HTTP/1.1"
connectionTimeout="120000"
redirectPort="8443" />
Nginx settings
If the server uses Nginx as a reverse proxy
to forward requests, it needs to set a timeout in the Nginx configuration file nginx.conf, otherwise it will return "java.io.IOException: The software in your host aborted an established "Connection" error message.
When not set, the Nginx response time defaults to 60 seconds. Here I configure the http headers keepalive_timeout
, client_header_timeout
, client_body_timeout
, send_timeout
, and the server code block proxy_read_timeout to 120 seconds.
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 100m;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 120; #Connection timeout, the server will close the connection after this time
send_timeout 120; #Send timeout
client_header_timeout 120; #Request header timeout
client_body_timeout 120; #Request body read timeout
#gzip on;
.....
#Configuration of business systems
server {
listen 9092;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8811/mywebsev/;
proxy_read_timeout 120; # Waiting for backend server response time
}
}
}